import O1HD, { managers } from "../../engines/gameEngine";
import { CsvFileKind } from "../../engines/libs/CsvFileKind";
import CsvDataManage from "../../engines/managers/CsvDataManage";
import level_peopleArchery from "../../levelBundles/level_PeopleArchery/level_peopleArchery";
import level_peopleArchery_exitPage from "../../levelBundles/level_PeopleArchery/page/level_peopleArchery_exitPage";
import PageBase from "../Base/PageBase";
import { PAGE } from "../Enum";
import { IPagePramsBase } from "../Interface";
import mainRoot from "../MainRoot";
import InGamePage from "./InGamePage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class peopleArcheryInGamePage extends InGamePage {

    @property({type:cc.Label,tooltip:"加攻击按钮"})
    lb_atkAddition:cc.Label = null;

    @property({type:cc.Label,tooltip:"进度文本"})
    lb_schedule:cc.Label = null;

    @property({type:cc.Node,tooltip:"titleNode"})
    titleNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"武器item"})
    weaponItem:cc.Node = null;

    @property({type:cc.Node,tooltip:"武器content"})
    weaponContent:cc.Node = null;

    @property({type:cc.Node,tooltip:"可以合成的背景图片"})
    canSynthSpf:cc.SpriteFrame = null;

    @property({type:cc.Node,tooltip:"不可以合成的背景图片"})
    noSynthSpf:cc.SpriteFrame = null;

    level:level_peopleArchery = null;

    start(): void {
        this.scheduleOnce(()=>{
            cc.tween(this.titleNode).to(1,{opacity:0}).start();
        },3);
        this.tipBtn.active = false;
        
        this.tipsShow.string = this.tipsShow.string.replace("x",(O1HD.getManagers().peopleArcheryManager.getData().curInfinityLevel + 1).toString());
    }

    bindLevel(level:level_peopleArchery){
        this.level = level;
        this.refrenshAdditionLb();
    }

    refrenshAdditionLb(){
        let atkAddition:number = this?.level?.peopleArcheryData["video_atk_num"] || 0;
        this.lb_atkAddition.string = `+${atkAddition}`;
    }

    onBtnAtkAddition(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        if(O1HD.getServices().userSrv.getCoins() >0){
            O1HD.getServices().userSrv.setCoins(-1,true);
            let atkAddition:number = this.level.role.atk;
            this.level.parseDataAddPeople({param_type:"1",option_param:atkAddition})
        }else{
            O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
                let atkAddition:number = this.level.role.atk;
                this.level.parseDataAddPeople({param_type:"1",option_param:atkAddition})
            },()=>{

            },null,"看广告加双倍攻击");
        }
    }

    onBtnHpAddition(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        if(O1HD.getServices().userSrv.getCoins() >0){
            O1HD.getServices().userSrv.setCoins(-1,true);
            let hpAddition:number = this.level.role.hp;
            let maxHpAddition:number = this.level.role.maxHp;
            this.level.role.maxHp = maxHpAddition * 2;
            this.level.role.hp = hpAddition * 2;
            this.level.roleAddParamLb({param_type:"0",option_param:hpAddition,additionFlag:false});
        }else{
            O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
                let hpAddition:number = this.level.role.hp;
                let maxHpAddition:number = this.level.role.maxHp;
                this.level.role.maxHp = maxHpAddition * 2;
                this.level.role.hp = hpAddition * 2;
                this.level.roleAddParamLb({param_type:"0",option_param:hpAddition,additionFlag:false});
            },()=>{

            },null,"看广告加双倍血量");
        }
    }

    async onBtnExit(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        let Prams: IPagePramsBase = {
            mode: this.m_mode,
        }
        let page = await mainRoot.UISystem.OpenPage(PAGE.PEOPLE_ARCHERY_EXIT_PAGE,Prams);
        (page as level_peopleArchery_exitPage).bindLevel(this.level);
    }

    refrenshScedule(){
        this.lb_schedule.string = this.level ? `${ (this.level.curWave / this.level.maxWave * 100).toFixed(1)}%` :"0%";
    }

    update(dt: number): void {
        this.refrenshScedule();    
    }

    refrenshWeaponContent(){
        let weapons = this.level.role.getWeapons();
        let noRepeatAry:string[] = [];
        for(let weapon of weapons){
            if(!noRepeatAry.includes(weapon)){
                noRepeatAry.push(weapon);
            }
        }
        let needCount:number = noRepeatAry.length - this.weaponContent.childrenCount;
        for(let i = 0;i<needCount;i++){
            let item:cc.Node = cc.instantiate(this.weaponItem);
            item.active = true;
            item.setParent(this.weaponContent);
            item.setPosition(cc.v2(0,0));
        }

        let numAry:number[] = new Array<number>(noRepeatAry.length).fill(0);
        weapons.forEach((weaponId:string)=>{
            let index = noRepeatAry.indexOf(weaponId);
            if(index!= -1){
                numAry[index]++;
            }
        });

        this.weaponContent.children.forEach((item:cc.Node,index:number)=>{
            // let lbWeaponName:cc.Label = item.getChildByName("lbName").getComponent(cc.Label);
            let weaponData = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.peopleArcheryMagicWeapon,noRepeatAry[index]);
            let iconPath:string = weaponData["icon_path"];
            let sp:cc.Sprite = item.getChildByName("icon").getComponent(cc.Sprite);
            mainRoot.LevelSystem.getOneTypeBundle(this.level.getLevelInfo().levelType).load(`selectWeapon/${iconPath}`,cc.SpriteFrame,(err,spf:cc.SpriteFrame)=>{
                if(err){
                    console.log("加载道具icon失败",err);
                    return;
                }
                sp.spriteFrame = spf;
            });
            let bgSp:cc.Sprite = item.getChildByName("bg").getComponent(cc.Sprite);
            bgSp.spriteFrame = weaponData["arise_flag"] == "0" ? this.canSynthSpf : this.noSynthSpf;
            let effectNode:cc.Node = item.getChildByName("effect");
            effectNode.active = weaponData["arise_flag"] == "0";
            // let tween = cc.tween(effectNode).to(5,{angle:360}).call(()=>{
            //     effectNode.angle = 0;
            // });
            // cc.tween(effectNode).repeatForever(tween).start();
            // lbWeaponName.string = name;
            let lbWeaponNum:cc.Label = item.getChildByName("lbNum").getComponent(cc.Label);
            lbWeaponNum.string = `×${numAry[index]}`;
        });
    }

}
